package com.scalar.chiptrack.adaptor.ikanos.gtc;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.TestLotBinReport;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.SortLotBinData;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.XLSParserImpl;
import com.scalar.chiptrack.adaptor.gtc.GTCWipInfo;
import com.scalar.chiptrack.adaptor.gtc.GTCShipInfo;
import com.scalar.chiptrack.adaptor.gtc.GTCSortBinReport;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.operations.dataentry.InTransitLots;
import com.scalar.ScalarException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Locale;
import java.util.Iterator;
import java.util.Date;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import jxl.Cell;
import jxl.Workbook;
import jxl.Sheet;
import jxl.CellType;
import jxl.DateCell;

/**
 * User: ChandraSekhar
 * Date: Jul 16, 2008
 * Time: 6:42:22 PM
 */
public class GTCDataParser extends JobHandler implements AdaptorConstants
{
    private static Logger m_parserLogger    = null;
    private String m_dataOutputDir          = null;

    private HashMap<String, FabSortLot> m_sortWipDataMap        = new HashMap<String, FabSortLot>();
    private HashMap<String, ShippedLot> m_sortShipDataMap       = new HashMap<String, ShippedLot>();
	private HashMap<String, InventoryLot> m_waferBankDataMap	= new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_preTestDataMap		= new HashMap<String, InventoryLot>();
    private HashMap<String, TestLot> m_ftDataMap		        = new HashMap<String, TestLot>();
    private HashMap<String, InventoryLot> m_eolDataMap		    = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_fgDataMap		    = new HashMap<String, InventoryLot>();
    private HashMap<String, ShippedLot> m_fgShipDataMap		    = new HashMap<String, ShippedLot>();
    private HashMap<String, FabSortLot> m_sortBinDataMap		= new HashMap<String, FabSortLot>();
    private HashMap<String, FabSortLot> m_sortTotalMap      = new HashMap<String, FabSortLot>();

    int m_totalProbeQty         = 0;
    int m_totalPassQty          = 0;
    float m_totalYield          = 0.0f;
    int m_waferQtyIn            = 0;
    
    public GTCDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_GTC, LOCATION_TYPE_TEST );

            if ( m_dataOutputDir != null )
            {
                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );

                m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_GTC );
                FabSortAdaptorDAO.setLogger( m_parserLogger );   // All the DAOs and Parser should be logging into same file
                InventoryLotDAO.setLogger( m_parserLogger );
                TestAdaptorDAO.setLogger( m_parserLogger );
                ShippedLotDAO.setLogger( m_parserLogger );
                AdaptorUtils.setLogger( m_parserLogger);
            }
        }
        catch ( ScalarException se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading GTC output/log directory details from database, Exception: " + se );
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        new GTCDataParser().execute();
    }

    /**
     *
     */
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_GTC, CONSTANT_PARSER);
        
        File doneDestDir = new File( m_dataOutputDir + "/done" );
        File dataoutDir  = new File( m_dataOutputDir );

        File[] fileList = dataoutDir.listFiles();

        if ( ( fileList == null ) || (fileList.length <= 3))
        {
            log("No file found in directory: " + dataoutDir);
            System.out.println("No file found in directory: " + dataoutDir );
            throw new ScalarException("No file found in directory: " + dataoutDir+ "----\n\n" );
        }

        ArrayList<File> successFileList   = new ArrayList<File>();
        boolean success             = false;

        try
        {
            for ( int i = 0; i < fileList.length; i++ )
            {
                if ( fileList[i].isDirectory() == true )
                {
                    continue;
                }

                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith(GTCPOP3Adaptor.FILE_NAME_CP_WIP ) )
                {
                    ArrayList wipsheetDataList = parseCPWipSheetInfo( fileList[i].getAbsolutePath() );

                    if ( wipsheetDataList != null )
                    {
                        parseSortProcessWipList( wipsheetDataList );
                    }

                    successFileList.add( fileList[i] );
                }
                if ( fileList[i].getName() != null  &&  fileList[i].getName().trim().startsWith(GTCPOP3Adaptor.FILE_NAME_CP_SHIP ) )
                {
                    ArrayList shipsheetDataList = parseCPShipSheetInfo( fileList[i].getAbsolutePath() );

                    if ( shipsheetDataList != null )
                    {
                        parseSortProcessShipList( shipsheetDataList );
                    }

                    successFileList.add( fileList[i] );
                }
                if ( fileList[i].getName() != null  && ( fileList[i].getName().trim().startsWith(GTCPOP3Adaptor.FILE_NAME_FT_WIP ) ) )
                {
                    ArrayList ftwipsheetDataList = parseFTWipInfo( fileList[i].getAbsolutePath() );

                    if ( ftwipsheetDataList != null )
                    {
                        parseFtProcessWipList( ftwipsheetDataList );
                    }

                    successFileList.add( fileList[i] );
                }
                if ( fileList[i].getName() != null  && fileList[i].getName().startsWith( GTCPOP3Adaptor.FILE_NAME_CP_BINNING ) )
                {
                    /*ArrayList sortlotbinList = parseSortLotBinReport( fileList[i].getAbsolutePath() );


                    //To get the Sort Binning information
                    getSortBinDetails( sortlotbinList );
                    m_totalPassQty  = 0;
                    m_totalProbeQty = 0;*/

                      //For Saving the Sort Binning
                    ArrayList<SortLotBinData> sortBinList = parseCPBinReport( fileList[i].getAbsolutePath() );

                    if ( sortBinList != null && sortBinList.size() > 0 )
                    {
                        log("updateing the GTC Cp Binning Info....");
                        FabSortAdaptorDAO.saveSortBinInfo( null, sortBinList, VENDOR_NAME_GTC );
                        success = true;
                    }

                    successFileList.add(fileList[i]);
                }
               if ( fileList[i].getName() != null
                       && ( fileList[i].getName().trim().startsWith( GTCPOP3Adaptor.FILE_NAME_DAILY_YIELD )
                       || fileList[i].getName().trim().startsWith("IKANOS_FT_MTD_YIELDREPORT") ) )
               {
                    ArrayList testBinList = parseFTYieldReport( fileList[i].getAbsolutePath() );

                   if ( testBinList != null && testBinList.size() > 0 )
                    {
                        //to get the GTC Test Bin Details
                        if ( testBinList.isEmpty() == false )
                        {
                            log( "Updating GTC Binning Information" );
                            TestAdaptorDAO.saveTestBinInfo ( null, testBinList , VENDOR_NAME_GTC );
                            success = true;
                        }
                        else
                        {
                            log( "GTC Binning Information List is Empty" );
                        }
                    }
                   successFileList.add( fileList[i] );
               }

               if ( fileList[i].getName() != null  && ( fileList[i].getName().trim().startsWith(GTCPOP3Adaptor.FILE_NAME_FG_WIP ) ) )
                {
                    ArrayList fgwipsheetDataList = parseFGWipInfo( fileList[i].getAbsolutePath() );

                    if ( fgwipsheetDataList != null )
                    {
                        parseFGProcessWipList( fgwipsheetDataList );
                    }

                    successFileList.add( fileList[i] );
                }

                if ( fileList[i].getName() != null  && ( fileList[i].getName().trim().startsWith(GTCPOP3Adaptor.FILE_NAME_FT_SHIP ) ) )
                {
                    ArrayList fgshipsheetDataList = parseFGShipInfo( fileList[i].getAbsolutePath() );

                    if ( fgshipsheetDataList != null )
                    {
                        parseFGProcessShipList( fgshipsheetDataList );
                    }

                    successFileList.add( fileList[i] );
                }
            }
        }
        catch ( Exception e )
        {
            success = false;
            log( "ERROR!! ERROR!! ERROR!! While parsing and storing File " + e );
            e.printStackTrace();
            throw e;
        }

		/*Updating the Wafer Store Lot Information*/
        try
        {
            // Save Wafer Store Information
            if (m_waferBankDataMap.isEmpty() == false)
            {
                log(" Updating Wafer Store Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_waferBankDataMap.values().iterator(), LOCATION_TYPE_WAFER_BANK, VENDOR_NAME_GTC);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_WAFER_BANK, VENDOR_NAME_GTC);
            }
            success = true;
            m_waferBankDataMap.clear();
            m_waferBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Wafer Store Lot information into database: " + se);
            throw se;
        }
        //SAVING BEFORE THE SORT WIP TO AVOID THE PARTIAL BINNING, IF STILL PARTIAL QTY IS IN WIP, for showing this as WIP 
        try
        {
            if ( m_sortBinDataMap.isEmpty() == false )
            {
                FabSortAdaptorDAO.saveSortBinData( null, m_sortBinDataMap.values().iterator() );
            }
            success = true;
            m_sortBinDataMap.clear();
        }
        catch(Exception e)
        {
            success = false;
            System.out.println(" Exception Occrred While Saving the Sort Binning Info " +e );
            throw e;
        }
        // to save the Sort lot tables
        try
        {
            if ( m_sortWipDataMap.isEmpty() == false )
            {
                log( "Updating Sort Lot Information" );
                FabSortAdaptorDAO.saveSortLot( null, m_sortWipDataMap.values().iterator(),WIP_TYPE_SORT, VENDOR_NAME_GTC );
                success = true;
            }
            else
            {
                FabSortAdaptorDAO.closeFabSortLot( null,WIP_TYPE_SORT, VENDOR_NAME_GTC );
            }

        }
        catch ( Exception e )
        {
            success = false;
            log( "ERROR!! ERROR!! ERROR!! Could not update Global Test Sort Data information into database: " + e );
            e.printStackTrace();
            throw e;
        }

        try
        {
            // Save Sort Information
            if ( m_sortShipDataMap.isEmpty() == false )
            {
                log( " Updating Sort Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_sortShipDataMap.values().iterator() );
            }
            success = true;
            m_sortShipDataMap.clear();
            m_sortShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Sort Shipping Lot information into database: " + se );
            throw se;
        }

        try
        {
           if ( m_preTestDataMap.isEmpty() == false )
           {
               log(" Updating Class Stroe Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_preTestDataMap.values().iterator(), LOCATION_TYPE_PRE_TEST, VENDOR_NAME_GTC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_PRE_TEST, VENDOR_NAME_GTC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Pre Test Lot information into database: " + se);
          throw se;
      }
         // Saving the Test Lot Information
        try
        {
            // Save Final Test Information
            if ( m_ftDataMap.isEmpty() == false )
            {
                log( " Updating Test Lot Information" );
                TestAdaptorDAO.saveTestLot( null, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_GTC );
            }
            else
            {
                TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_GTC );
            }

            success = true;
            m_ftDataMap.clear();
            m_ftDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se );
            throw se;
        }
        // to save the EOL lot
         try
         {
             if ( m_eolDataMap.isEmpty() == false )
             {
                 log(" Updating EOL Lot Information");
                 InventoryLotDAO.saveInventoryLot( null, m_eolDataMap.values().iterator(), LOCATION_TYPE_EOL, VENDOR_NAME_GTC );
                 success = true;
             }
             else
             {
                 InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_EOL, VENDOR_NAME_GTC  );
             }
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update EOL Lot information into database: " + se);
            throw se;
        }
         // to save the FG lot tables
       try
       {
           if ( m_fgDataMap.isEmpty() == false )
           {
               log(" Updating Finshed Goods Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, VENDOR_NAME_GTC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_FG, VENDOR_NAME_GTC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Finshed Goods Lot information into database: " + se);
          throw se;
      }

      try
        {
            // Save FG Ship Information
            if ( m_fgShipDataMap.isEmpty() == false )
            {
                log( " Updating FG Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_fgShipDataMap.values().iterator() );
            }
            success = true;
            m_fgShipDataMap.clear();
            m_fgShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update FG Shipping Lot information into database: " + se );
            throw se;
        }
        //To close the Drop out lots
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_GTC );

        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_GTC, true, true, true );

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( GTCPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, GTCPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, GTCPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }

       // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }//end of execute method


    private  ArrayList parseCPWipSheetInfo( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList wipSheetList  = new ArrayList();
        String line             = null;
        GTCWipInfo  wipStatus   = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Global Test Wip Info..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Global Test Wip Info..." );

            Workbook workbook = Workbook.getWorkbook( detailWipFile );
            Sheet sheet = workbook.getSheet( 0 );
            Cell[] row = null;

            for ( int rowCount = 3; rowCount < sheet.getRows(); rowCount++ )
            {
                row = sheet.getRow( rowCount );

                wipStatus = new GTCWipInfo();

                if( row.length > 0 )
                {
                    if ( row[0].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setPartNumber( null );
                    }
                    else
                    {
                        wipStatus.setPartNumber( row[0].getContents().trim() );
                    }
                }
                if( row.length > 1 )
                {
                    if ( row[1].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setFablotNumber( null );
                    }
                    else
                    {
                        wipStatus.setFablotNumber( row[1].getContents().trim() );
                    }
                }
                if( row.length > 2)
                {
                    if ( row[2].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setLotNumber( null );
                    }
                    else
                    {
                        wipStatus.setLotNumber(row[2].getContents().trim());
                    }
                }
                if( row.length > 3)
                {
                    if ( row[3].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setReceiveDate( null );
                    }
                    else if( row[3].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[3];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setReceiveDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setReceiveDate(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {
                        wipStatus.setReceiveDate( row[3].getContents().trim());
                    }
                }
                //RoW[4]  Received From (Fab Vendor) Not Required
                if ( row.length > 5 )
                {
                   if ( row[5].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setReceivedQty( null );
                    }
                    else
                    {
                        wipStatus.setReceivedQty( row[5].getContents().trim());
                    }
                }
                // 6 Gross Die Not Required
                if( row.length > 7 )
                {
                    if ( row[7].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setWaferQtyIn( null );
                    }
                    else
                    {
                        wipStatus.setWaferQtyIn( row[7].getContents().trim());
                    }

                }
                // 8 STARTING Die Qty Not Required
                if( row.length > 9 )
                {
                    if ( row[9].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setDateIn( null );
                    }
                    else if( row[9].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[9];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setDateIn( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setDateIn(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {
                        wipStatus.setDateIn( row[9].getContents().trim());
                    }
                }
                // ROW[10] Wip Die Qty not Required
                if ( row.length > 11 )
                {
                    if ( row[11].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setStatus( null );
                    }
                    else
                    {
                        wipStatus.setStatus( row[11].getContents().trim() );
                    }
                }
                if( row.length > 12 )
                {
                    if ( row[12].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setStage( null );
                    }
                    else
                    {
                        wipStatus.setStage( row[12].getContents().trim());
                    }
                }
                if( row.length > 13 )
                {
                    if ( row[13].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setHoldReason( null );
                    }
                    else
                    {
                        wipStatus.setHoldReason( row[13].getContents().trim());
                    }
                }
                if( row.length >14 )
                {
                    if ( row[14].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setTestPgm( null );
                    }
                    else
                    {
                        wipStatus.setTestPgm( row[14].getContents().trim());
                    }
                }

                wipSheetList.add( wipStatus );
            }
        }
        catch ( Exception ex )
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the GTC SORT WIP sheet");
            ex.printStackTrace();
            throw ex;
        }
        return wipSheetList;
    }

    private  ArrayList parseFTWipInfo( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList statusList    = new ArrayList();
        String line             = null;

        try
        {
            File wipInfoFile = new File( fileName );
            if ( wipInfoFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for FT WIP Status Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for FT WIP Status Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( fileName ) );

            reader.readLine();  // skip 1st line
            reader.readLine();  // skip 2nd line

            while ( ( line = reader.readLine() ) != null )
            {
                GTCWipInfo wipInfo = new GTCWipInfo();

                line = line.replaceAll( "\"", "" );

                String[] token = line.split(",");

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                             //case 0:   Work Order
                            case 1:
                                wipInfo.setPartNumber( token[x].trim());
                                break;

                            //case 2:
                               
                            case 3:
                                wipInfo.setPinCount(token[x].trim());
                                break;
                            case 4:
                                wipInfo.setDateCode(token[x].trim());
                                break;
                            case 5:
                                wipInfo.setLotNumber(token[x].trim());
                                break;
                            case 6:
                                wipInfo.setFablotNumber(token[x].trim());
                                break;
                            case 7:
                                wipInfo.setReceivedQty(token[x].trim());
                                break;
                             case 8:
                                wipInfo.setReceiveDate(token[x].trim());
                                break;
                            case 9:
                               wipInfo.setLotQty ( token[x].trim());
                                break;
                            case 10:
                                wipInfo.setDateIn( token[x].trim() );
                                break;
                             case 11:
                                wipInfo.setExpectedDateOut( token[x].trim() );
                                break;
                             case 12:
                                wipInfo.setFtOutDate( token[x].trim() );
                                break;
                             case 13:
                                wipInfo.setStage( token[x].trim() );
                                break;
                            case 14:
                                wipInfo.setStatus( token[x].trim() );
                                break;
                            /*case 15:
                                wipInfo.setDieQtyIn( token[x].trim() );
                                break;*/
                            case 15:
                                wipInfo.setTestPgm( token[x].trim() );
                                break;
                        }
                    }
                }
                if ( wipInfo.getPartNumber() != null && wipInfo.getPartNumber().length() > 0 &&
                     wipInfo.getFablotNumber() != null && wipInfo.getFablotNumber().length() > 0 )
                {
                    statusList.add( wipInfo );
                }
            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch (Exception e)
        {
            System.out.println("Exception while parsing TEST Wip Info: " + e);
            System.out.println("Line: " + line);
            throw new ScalarException("Exception while parsing TEST Wip Info: " + e + "\nLine: " + line, e);
        }

        return statusList;
    }

    private  ArrayList parseFGWipInfo( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList statusList    = new ArrayList();
        String line             = null;

        try
        {
            File wipInfoFile = new File( fileName );
            if ( wipInfoFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for FG WIP Status Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for FG WIP Status Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( fileName ) );

            reader.readLine();  // skip 1st line
            reader.readLine();  // skip 2nd line

            while ( ( line = reader.readLine() ) != null )
            {
                GTCWipInfo wipInfo = new GTCWipInfo();

                line = line.replaceAll( "\"", "" );

                String[] token = line.split(",");

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                            case 0:
                                wipInfo.setPartNumber( token[x].trim());
                                break;
                            case 2:
                                wipInfo.setLotNumber(token[x].trim());
                                break;
                            case 5:
                                wipInfo.setFablotNumber(token[x].trim());
                                break;
                            case 6:
                                wipInfo.setPkgType(token[x].trim());
                                break;
                            case 7:
                                wipInfo.setDateCode(token[x].trim());
                                break;
                            //8 Release NO
                            case 9:        //2008-10-04-11.08.54
                                 wipInfo.setReceiveDate( StringUtils.formatDate(token[x].trim(), "yyyy-MM-dd-hh.mm.ss", "MM/dd/yyyy"));
                                break;
                            case 10:
                                if ( token[x].trim().indexOf(".") != -1 )
                                {
                                    wipInfo.setReceivedQty( token[x].trim().substring(0, token[x].trim().indexOf(".") ) );
                                }
                                else
                                {
                                    wipInfo.setReceivedQty( token[x].trim() );
                                }
                                break;
                            //11 Ship Qty
                            case 12:
                                if ( token[x].trim().indexOf(".") != -1 )
                                {
                                    wipInfo.setDieQtyIn( token[x].trim().substring(0, token[x].trim().indexOf(".") ) );
                                }
                                else
                                {
                                    wipInfo.setDieQtyIn( token[x].trim() );
                                }
                                break;
                        }
                    }
                }
                if ( wipInfo.getPartNumber() != null && wipInfo.getPartNumber().length() > 0 &&
                     wipInfo.getFablotNumber() != null && wipInfo.getFablotNumber().length() > 0 )
                {
                    statusList.add( wipInfo );
                }
            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch (Exception e)
        {
            System.out.println("Exception while parsing FG Wip Info: " + e);
            System.out.println("Line: " + line);
            throw new ScalarException("Exception while parsing FG Wip Info: " + e + "\nLine: " + line, e);
        }

        return statusList;
    }

    private  ArrayList parseCPShipSheetInfo( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList statusList    = new ArrayList();
        String line             = null;

        try
        {
            File wipInfoFile = new File( fileName );
            if ( wipInfoFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for CP SHIP Status Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for CP SHIP Status Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( fileName ) );

            reader.readLine();  // skip 1st line

            while ( ( line = reader.readLine() ) != null )
            {
                GTCShipInfo shipInfo = new GTCShipInfo();

                String[] lineToken = line.split("\"");
                
                if (lineToken != null && lineToken.length > 1)
                {                       
                    lineToken[1] = lineToken[1].replaceAll(",", "");
                    if ( lineToken.length >= 3 )
                    {
                        lineToken[3] = lineToken[3].replaceAll(",", ";");
                    }
                }

                line = "";
                for (int i = 0; i < lineToken.length; i++)
                {
                    line = line + lineToken[i];
                }

                line = line.replaceAll( "\"", "" );

                String[] token = line.split(",");

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                            case 0:
                                shipInfo.setLotNumber( token[x].trim());
                                break;
                            case 1:
                                shipInfo.setCustPartNo( token[x].trim() );
                                break;
                            case 2:
                                shipInfo.setShipDate(StringUtils.formatDate(token[x].trim(), "yyyy/MM/dd","MM/dd/yyyy"));
                                break;
                            case 3:
                                shipInfo.setIncomingWaferQty(token[x].trim());
                                break;
                            case 4:
                                shipInfo.setShipDieQty( token[x].trim());
                                break;
                            case 5:
                                shipInfo.setShipWaferQty(token[x].trim());
                                break;
                            //6, 7
                             case 8:
                                shipInfo.setShipToLocation( token[x].trim() );
                                break;
                            case 9:
                                shipInfo.setWaferID( token[x].trim().replaceAll("[^0-9,;]", "") );
                                break;
                        }
                    }
                }
                if ( shipInfo.getCustPartNo() != null && shipInfo.getCustPartNo().length() > 0 &&
                     shipInfo.getLotNumber() != null && shipInfo.getLotNumber().length() > 0 )
                {
                    statusList.add( shipInfo );
                }
            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch (Exception e)
        {
            System.out.println("Exception while parsing Sort Shipping Info: " + e);
            System.out.println("Line: " + line);
            throw new ScalarException("Exception while parsing Sort Shipping Info: " + e + "\nLine: " + line, e);
        }

        return statusList;
    }

    private ArrayList parseFTYieldReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList ftYieldList  = new ArrayList();
        TestLotBinReport ftYieldStatus  = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for GTC FT Yield Info..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for GTC FT Yield Info..." );

            Workbook workbook = Workbook.getWorkbook( detailWipFile );
            Sheet sheet = workbook.getSheet( 0 );
            Cell[] row = null;

            for ( int rowCount = 3; rowCount < sheet.getRows(); rowCount++ )
            {
                row = sheet.getRow( rowCount );

                ftYieldStatus = new TestLotBinReport();
                //ROW 0, 1 not Required
                if( row.length > 2 )
                {
                    if ( row[2].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setDeviceName( null );
                    }
                    else
                    {
                        ftYieldStatus.setDeviceName( row[2].getContents().trim() );
                    }
                }
                if( row.length > 3 )
                {
                    if ( row[3].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setPkgType( null );
                    }
                    else
                    {
                        ftYieldStatus.setPkgType( row[3].getContents().trim() );
                    }
                }
                if( row.length > 4)
                {
                    if ( row[4].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setLeadCount( null );
                    }
                    else
                    {
                        ftYieldStatus.setLeadCount(row[4].getContents().trim());
                    }
                }
                if( row.length > 5)
                {
                    if ( row[5].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setCustLotNo( null );
                    }
                    else
                    {
                        ftYieldStatus.setCustLotNo( row[5].getContents().trim());
                    }
                }
                 //Row 6, 7, 8
                if ( row.length > 9 )
                {
                   if ( row[9].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setLotNumber( null );
                    }
                    else
                    {
                        ftYieldStatus.setLotNumber( row[9].getContents().trim());
                    }
                }

                if( row.length > 10 )
                {
                    if ( row[10].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setTesterId( null );
                    }
                    else
                    {
                        ftYieldStatus.setTesterId( row[10].getContents().trim());
                    }
                }
                //11
                if( row.length > 12 )
                {
                    if ( row[12].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setHandler( null );
                    }
                    else
                    {
                        ftYieldStatus.setHandler( row[12].getContents().trim());
                    }
                }
                if( row.length > 13 )
                {
                    if ( row[13].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setLb( null );
                    }
                    else
                    {
                        ftYieldStatus.setLb( row[13].getContents().trim());
                    }
                }

                if( row.length >14 )
                {
                    if ( row[14].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setTestProg( null );
                    }
                    else
                    {
                        ftYieldStatus.setTestProg( row[14].getContents().trim());
                    }
                }
                if( row.length >15 )
                {
                    if ( row[15].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setTemp( null );
                    }
                    else
                    {
                        ftYieldStatus.setTemp( row[15].getContents().trim());
                    }
                }
                if( row.length >16 )
                {
                    if ( row[16].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setDateIn( null );
                    }
                    else if( row[16].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[16];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            ftYieldStatus.setDateIn( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            ftYieldStatus.setDateIn(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {
                        ftYieldStatus.setDateIn( StringUtils.formatDate(row[16].getContents().trim(), "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss" ) );
                    }
                }
                if( row.length >17 )
                {
                    if ( row[17].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setDateOut( null );
                    }
                    else if( row[17].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[17];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            ftYieldStatus.setDateOut( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            ftYieldStatus.setDateOut(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {
                        ftYieldStatus.setDateOut(StringUtils.formatDate(row[17].getContents().trim(), "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss" ));
                    }
                }
                if( row.length > 18 )
                {
                    if ( row[18].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setStage( null );
                    }
                    else
                    {
                        ftYieldStatus.setStage( row[18].getContents().trim());
                    }
                }
                if( row.length > 19 )
                {
                    if ( row[19].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setInQty( null );
                    }
                    else
                    {
                        ftYieldStatus.setInQty( row[19].getContents().trim());
                    }
                }
                if( row.length > 20 )
                {
                    if ( row[20].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setPassQty( null );
                    }
                    else
                    {
                        ftYieldStatus.setPassQty( row[20].getContents().trim());
                    }
                }
                if( row.length > 21 )
                {
                    if ( row[21].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setYield( null );
                    }
                    else
                    {
                        ftYieldStatus.setYield( row[21].getContents().trim().replaceAll("%", ""));
                    }
                }

                if( row.length > 22 )
                {
                    if ( row[22].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setOsQty( null );
                    }
                    else
                    {
                        ftYieldStatus.setOsQty( row[22].getContents().trim());
                    }
                }
                if( row.length > 23 )
                {
                    if ( row[23].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setOSRate( null );
                    }
                    else
                    {
                        ftYieldStatus.setOSRate( row[23].getContents().trim().replaceAll("%", ""));
                    }
                }

                if( row.length > 24 )
                {
                    if ( row[24].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin1( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin1( row[24].getContents().trim());
                    }
                }
                if( row.length > 25 )
                {
                    if ( row[25].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin2( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin2( row[25].getContents().trim());
                    }
                }
                if( row.length > 26 )
                {
                    if ( row[26].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin3( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin3( row[26].getContents().trim());
                    }
                }
                if( row.length > 27 )
                {
                    if ( row[27].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin4( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin4( row[27].getContents().trim());
                    }
                }
                if( row.length > 28 )
                {
                    if ( row[28].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin5( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin5( row[28].getContents().trim());
                    }
                }
                if( row.length > 29)
                {
                    if ( row[29].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin6( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin6( row[29].getContents().trim());
                    }
                }
                if( row.length > 30 )
                {
                    if ( row[30].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin7( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin7( row[30].getContents().trim());
                    }
                }
                if( row.length > 31 )
                {
                    if ( row[31].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setBin8( null );
                    }
                    else
                    {
                        ftYieldStatus.setBin8( row[31].getContents().trim());
                    }
                }

                if( row.length > 32 )
                {
                    if ( row[32].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setVmQty( null );
                    }
                    else
                    {
                        ftYieldStatus.setVmQty( row[32].getContents().trim());
                    }
                }
                if( row.length > 33 )
                {
                    if ( row[33].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setLossQty( null );
                    }
                    else
                    {
                        ftYieldStatus.setLossQty( row[33].getContents().trim());
                    }
                }
                if( row.length > 34 )
                {
                    if ( row[34].getType().equals(CellType.EMPTY) )
                    {
                        ftYieldStatus.setDateCode( null );
                    }
                    else
                    {
                        ftYieldStatus.setDateCode( row[34].getContents().trim());
                    }
                }
                if ( ftYieldStatus.getDeviceName() != null && ftYieldStatus.getDeviceName().length() > 0 )
                {
                    ftYieldList.add( ftYieldStatus );
                }    
            }
        }
        catch ( Exception ex )
        {   System.out.println("ERROR!!!ERROR!!! While Parsing the GTC FT Daily Yield Report");
            ex.printStackTrace();
            throw ex;
        }

        return ftYieldList;
    }

    private  ArrayList parseFGShipInfo( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList statusList    = new ArrayList();
        String line             = null;

        try
        {
            File wipInfoFile = new File( fileName );
            if ( wipInfoFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for FG SHIP Status Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for FG SHIP Status Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( fileName ) );

            reader.readLine();  // skip 1st line

            while ( ( line = reader.readLine() ) != null )
            {
                GTCShipInfo shipInfo = new GTCShipInfo();

                line = line.replaceAll( "\"", "" );

                String[] token = line.split(",");
                
                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                            case 0:
                                shipInfo.setLotNumber( token[x].trim());
                                break;
                            case 1:
                                shipInfo.setCustPartNo( token[x].trim() );
                                break;
                            case 2:
                                shipInfo.setShipDate(StringUtils.formatDate(token[x].trim(), "yyyy/MM/dd","MM/dd/yyyy"));
                                break;
                            case 3:
                                shipInfo.setPoNumber(token[x].trim());
                                break;
                            case 4:
                                shipInfo.setSoNumber(token[x].trim());
                                break;
                            case 5:
                                shipInfo.setSrNumber(token[x].trim());
                                break;
                            // 6, 7 Incoming Qty	GoodDie
                            case 8:
                                shipInfo.setShipDieQty( token[x].trim());
                                break;
                            //9,10 Fail	Box qty
                            case 11:
                                shipInfo.setShipToLocation( token[x].trim() );
                                break;
                        }
                    }
                }
                if ( shipInfo.getCustPartNo() != null && shipInfo.getCustPartNo().length() > 0 &&
                     shipInfo.getLotNumber() != null && shipInfo.getLotNumber().length() > 0 )
                {
                    statusList.add( shipInfo );
                }
            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch (Exception e)
        {
            System.out.println("Exception while parsing FG Shipping Info: " + e);
            System.out.println("Line: " + line);
            throw new ScalarException("Exception while parsing FG Shipping Info: " + e + "\nLine: " + line, e);
        }

        return statusList;
    }
    /**
     * Reads, parses the file
     */
    private ArrayList parseSortLotBinReport( String fileName ) throws Exception
    {
        ArrayList lotbinList = new ArrayList();
        GTCSortBinReport sortBinReport = null;
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        // Phase-I variables
        File lotbinFile = new File( fileName );
        if ( lotbinFile.exists() == false )
        {
            throw new ScalarException( fileName + " doesnt exist" );
        }

        //System.out.println("fileName = " + fileName);
        
        String DEVICE   = "Device      :";
        String LOTNO    = "Lot ID      :";
        String DATE     = "End Time    :";
        String WAFER_QTY_IN = "Total Wafers:";
        String TESTER     = "Tester#     :";
        String GROSS_DIE = "Gross Die   :";
        String TEST_PGM = "Job         :";
        String deviceName = null;
        String lotNo = null;
        String dateIn = null;
        String tester = null;
        String testPgm = null;
        String line = null;

        BufferedReader bReader = null;
        try
        {
            bReader = new BufferedReader( new FileReader( lotbinFile ) );

            while ( ( line = bReader.readLine() ) != null )
            {
                line = line.trim();
                //System.out.println("line = " + line);
                if ( line.startsWith("W#") )
                {
                    break;
                }
                if ( line.indexOf( DEVICE ) != -1 )
                {
                    deviceName = line.substring( DEVICE.length(),  line.indexOf( TEST_PGM )).trim();
                    //System.out.println("deviceName = " + deviceName);
                }
                if ( line.indexOf( TEST_PGM) != -1 )
                {
                    //System.out.println("line.indexOf( TEST_PGM)  = " + line.indexOf(TEST_PGM));
                    //System.out.println("line.lastIndexOf(TEST_PGM) = " + line.lastIndexOf(TEST_PGM));
                    testPgm = line.substring( line.indexOf(TEST_PGM) + TEST_PGM.length() ).trim() ;
                    //System.out.println("testPgm = " + testPgm);
                }
                if ( line.indexOf( LOTNO ) != -1 )
                {
                    lotNo = line.substring( LOTNO.length(), line.indexOf(WAFER_QTY_IN) ).trim();
                    //System.out.println("lotNo = " + lotNo);
                }

                if ( line.indexOf( WAFER_QTY_IN ) != -1 )
                {
                    m_waferQtyIn = Integer.parseInt( line.substring( line.indexOf(WAFER_QTY_IN) + WAFER_QTY_IN.length() ).trim() );
                    //System.out.println("waferQtyIn = " + m_waferQtyIn );
                }
                
                if ( line.indexOf( TESTER ) != -1 )
                {
                    tester = line.substring( TESTER.length(), line.indexOf("Operator#   :") ).trim() ;
                    //System.out.println("tester = " + tester);
                }

                if ( line.indexOf( GROSS_DIE ) != -1 )
                {
                    m_totalProbeQty = Integer.parseInt( line.substring( line.indexOf(GROSS_DIE) + GROSS_DIE.length() ).trim() ) ;
                    //System.out.println("m_totalProbeQty = " + m_totalProbeQty);
                    //System.out.println("GDPW = " + m_totalProbeQty/m_waferQtyIn );
                    //gdpw = m_totalProbeQty/m_waferQtyIn;
                }
                if ( line.indexOf( DATE ) != -1 )
                {
                    dateIn = line.substring( DATE.length() ).trim();
                    //dateIn = dateIn.substring(0,dateIn.indexOf(" "));
                    //System.out.println("dateIn = " + dateIn);
                }
            }
            bReader.close();
            bReader = null;
            line = null;

            ArrayList transitLots = AdaptorDAO.loadAllTransitLotsByProcess(LOCATION_TYPE_SORT, lotNo,LOCATION_TYPE_FAB);
            InTransitLots inTransitLots = null;

            int startLine = 0;
            int counter1 = 0;
            int counter2 = 0;
            int rejectedQty   = 0;
            bReader = new BufferedReader( new FileReader( lotbinFile ) );
            while ( ( line = bReader.readLine() ) != null  )
            {
                counter1++;
                line = line.trim();
                if ( line.startsWith( "W#" ) && line.indexOf("Total_Pass") != -1 )
                {
                    startLine = counter1;
                    break;
                }
            }
            bReader.close();
            bReader = null;
            line = null;

            bReader = new BufferedReader( new FileReader( lotbinFile ) );
            while ( ( line = bReader.readLine() ) != null )
            {
                counter2++;
                line = line.trim();
                //System.out.println("START line = " + line);

                if ( line.startsWith("BIN#") )
                {
                    break;
                }
                if ( line.length() > 0 && counter2 > startLine && ( !line.startsWith( "W#" ) ) && ( !line.startsWith( "------" ) ) && ( !line.startsWith( "Tol" ) ) && ( !line.startsWith( "Yld" ) ) )
                {
                    //System.out.println("line = " + line );

                    sortBinReport = new GTCSortBinReport();

                    int waferId = Integer.parseInt( line.substring( 0, 3 ).trim()  );

                    //System.out.println("waferId = " + waferId);
                    //System.out.println("line.substring(4, 7).trim() = " + line.substring(7, 14).trim());
                    int bin1QtyIn = Integer.parseInt(line.substring(line.length()-6, line.length()).trim()); //TOTAL PASS IS TAKING AS BIN1 QTY
                    //int bin2QtyIn = Integer.parseInt(line.substring(14, 19).trim()); //BIN2 QTY
                    //System.out.println("bin1QtyIn = " + bin1QtyIn);
                    
                    //System.out.println("bin2QtyIn = " + bin2QtyIn);
                    //float waferYield = Float.parseFloat(StringUtils.formatNumber( (bin1QtyIn*100.00f)/gdpw , "##.##"));
                    //System.out.println("waferYield = " + waferYield);
                    
                    //rejectedQty =  gdpw - bin1QtyIn;
                    //System.out.println("rejectedQty= " + rejectedQty);
                    m_totalPassQty = m_totalPassQty + bin1QtyIn;
                    boolean isRmaWafer = false;
                    if( lotNo != null && deviceName != null && waferId > 0 && waferId < 26 )
                    {
                        if ( transitLots != null && transitLots.size() > 0 )
                        {
                            for(int i=0; i< transitLots.size(); i++ )
                            {
                                inTransitLots = (InTransitLots) transitLots.get(i);

                                String[] waferIds = inTransitLots.getWaferId().split(";");
                                for (int wf=0; wf < waferIds.length; wf++)
                                {
                                    if ( (waferId+"").equalsIgnoreCase( waferIds[wf] ) )
                                    {
                                        isRmaWafer = true;
                                        break;
                                    }
                                }
                                if(isRmaWafer)
                                {
                                    break;
                                }
                            }
                        }

                        
                        sortBinReport.setWaferId( waferId +"" );

                        if(isRmaWafer)
                        {                               
                            sortBinReport.setGoodBinQty( 0 );
                        }
                        else
                        {
                            sortBinReport.setGoodBinQty( bin1QtyIn );
                        }

                        //sortBinReport.addBinNumber( 1,  bin1QtyIn);
                        //sortBinReport.addBinNumber( 2,  bin2QtyIn);

                        sortBinReport.setDeviceName( deviceName );
                        sortBinReport.setDateIn( dateIn );
                        sortBinReport.setLotNumber( lotNo );
                        sortBinReport.setRejectedQty( rejectedQty );
                        sortBinReport.setWaferYield( 0 );
                        sortBinReport.setTester( tester );
                        sortBinReport.setTestPgm( testPgm );

                        lotbinList.add(sortBinReport);
                    }
                }

            } //END OF WHILE
        }
        catch ( Exception e )
        {
            System.out.println( "Exception while parsing GTC Sort Bin information \n" + e + "\n" );
            throw e;
        }
        finally
        {
            if ( bReader != null )
            {
                try
                {
                    bReader.close();
                }
                catch ( IOException ignore )
                {
                }
            }
        }
        return lotbinList;
    } // end of prase sortLot bin report method

    private void getSortBinDetails( ArrayList sortBinList )  throws Exception
    {
        if ( sortBinList == null || sortBinList.size() == 0 )
        {
           log( "No Sort Bin Lots when the List is Empty");
           return;
        }
        GTCSortBinReport sortbinReport  = null;
        String partNumber                = null;
        String lotNumber                 = null;
        String waferId                   = null;
        FabSortLot sortBinLot            = null;

        try
        {
            Iterator iterator = sortBinList.iterator();

            while ( iterator.hasNext() )
            {
                sortbinReport = (GTCSortBinReport )iterator.next() ;

                lotNumber  = sortbinReport.getLotNumber();
                waferId    = sortbinReport.getWaferId();
                partNumber = sortbinReport.getDeviceName();

                if ( partNumber != null && lotNumber != null && waferId != null )
                {
                   sortBinLot = ( FabSortLot ) m_sortBinDataMap.get( partNumber + lotNumber + waferId);

                    if ( sortBinLot == null )
                    {
                        sortBinLot = new FabSortLot( VENDOR_NAME_GTC, LOCATION_TYPE_SORT );
                        m_sortBinDataMap.put( partNumber + lotNumber + waferId  , sortBinLot );
                    }

                    sortBinLot.setCustProduct( partNumber );
                    sortBinLot.setLotId( lotNumber );
                    sortBinLot.setWaferID( waferId );
                     // WAFER QTY IN Always Same(total wafers) ,But For Each every Wafer update the Wafer Qty, the the WaferQty
                    sortBinLot.setQtyIn( m_waferQtyIn +"" );
                    sortBinLot.setDateIn( sortbinReport.getDateIn() );
                    sortBinLot.setTotalgoodDieQtyIn( m_totalPassQty );
                    sortBinLot.setTotalprobeQtyIn( m_totalProbeQty );
                    sortBinLot.setTesterNo( sortbinReport.getTester());
                    sortBinLot.setTestProgram( sortbinReport.getTestPgm() );
                    sortBinLot.setGoodDieQtyIn(sortbinReport.getGoodBinQty());
                    sortBinLot.setRejectedQtyIn(sortbinReport.getRejectedQty() );
                    sortBinLot.setShipYield(sortbinReport.getWaferYield()+"");
                    sortBinLot.setTotalYield( m_totalYield ); // if sets as 0, in the PROC Yield Calculating (INQTY *100.00/OUTQTY)
                    sortBinLot.setBinNumber( "Bin1" );
               }
            }
        }
        catch(Exception e )
        {
            throw new ScalarException("Exception occured while parsing the Sort Bin Inforamtion" +e );
        }
    }
    private ArrayList<SortLotBinData> parseCPBinReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );
        ParserResult result     = null;

        String lotNumber        = null;
        String partNumber       = null;
        String fablotNumber     = null;
        FabSortLot sortBinTotalLot = null;
        FabSortLot sortBinLot   = null;
        ArrayList<SortLotBinData> cpYieldList  = new ArrayList<SortLotBinData>();
         int waferId             = 0;
        HashMap<String, Integer> waferIdMap = new HashMap<String, Integer>();
        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for CP Yield Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for CP Yield Report..." );

            Parser parser = ParserFactory.getXLSParser( detailWipFile );

            ((XLSParserImpl)parser).setHeaderRowIdx(0);
            ((XLSParserImpl)parser).setDataStartRowIdx(1);

            result = parser.parse();

            //System.out.println("result.getString() = " + result.getHeadings() );
             while ( result.next() )
            {
                partNumber = result.getString("Device");
                lotNumber = result.getString("Ikanons Lot No");
                waferId   = result.getInt("Wafer #");
                String insertion = result.getString("Insertion");

                if (partNumber!= null && partNumber.trim().length()>0 && lotNumber != null && lotNumber.trim().length() > 0 && waferId > 0 && waferId < 26 )
                {
                    sortBinTotalLot = (FabSortLot) m_sortTotalMap.get( partNumber + lotNumber + insertion);

                    if ( sortBinTotalLot == null )
                    {
                        sortBinTotalLot = new FabSortLot( VENDOR_NAME_GTC, LOCATION_TYPE_SORT );
                        m_sortTotalMap.put( partNumber + lotNumber+insertion  , sortBinTotalLot );
                    }

                    int m_totalProbeQty         = 0;
                    int m_totalPassQty          = 0;
                    int m_waferQtyIn            = 0;
                    if ( sortBinTotalLot.getQtyIn() != null )
                    {
                       try
                       {
                           m_waferQtyIn = Integer.parseInt( sortBinTotalLot.getQtyIn() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                       }

                       m_totalPassQty = sortBinTotalLot.getTotalgoodDieQtyIn();
                       m_totalProbeQty = sortBinTotalLot.getTotalprobeQtyIn();

                       if ( !waferIdMap.containsKey( partNumber + lotNumber + waferId+insertion ) )
                       {
                           waferIdMap.put( partNumber + lotNumber+waferId+insertion, waferId );
                            m_waferQtyIn += 1;

                            m_totalPassQty += result.getInt("good Dies");
                            m_totalProbeQty += result.getInt("GDPW");
                       }
                    }
                    else
                    {
                        if ( !waferIdMap.containsKey( partNumber + lotNumber+waferId + insertion ) )
                        {
                            waferIdMap.put( partNumber + lotNumber+waferId+ insertion, waferId );
                            m_waferQtyIn = 1;
                            m_totalPassQty = result.getInt("good Dies");
                            m_totalProbeQty = result.getInt("GDPW");
                        }
                    }
                    
                    sortBinTotalLot.setQtyIn( m_waferQtyIn+"");
                    sortBinTotalLot.setTotalgoodDieQtyIn( m_totalPassQty);
                    sortBinTotalLot.setTotalprobeQtyIn( m_totalProbeQty );
                }
            }
            //FOR Restting the Row Index
            result.reset();
            
            String lotNo = "";
            while ( result.next() )
            {
                partNumber   = result.getString("Device");
                fablotNumber = result.getString("Ikanons Lot No");
                lotNumber    = result.getString("Ikanons Lot No");
                waferId   = result.getInt("Wafer #");
                String insertion = result.getString("Insertion");
                if ( partNumber != null && partNumber.length() > 0 && lotNumber != null && lotNumber.length() > 0 && waferId > 0 && waferId < 26 )
                {
                     ArrayList transitLots  = null;

                     if (!lotNo.equalsIgnoreCase( lotNumber) )
                     {
                         transitLots = AdaptorDAO.loadAllTransitLotsByProcess(LOCATION_TYPE_SORT, lotNumber,LOCATION_TYPE_FAB);
                         lotNo = lotNumber;
                     }

                    boolean isRmaWafer = false;
                    SortLotBinData cpYieldStatus = new SortLotBinData();

                    sortBinLot = ( FabSortLot ) m_sortBinDataMap.get( partNumber + lotNumber + waferId);

                    if ( sortBinLot == null )
                    {
                        sortBinLot = new FabSortLot( VENDOR_NAME_GTC, LOCATION_TYPE_SORT );
                        m_sortBinDataMap.put( partNumber + lotNumber + waferId  , sortBinLot );
                    }

                    if ( transitLots != null && transitLots.size() > 0 )
                    {
                        for(int i=0; i< transitLots.size(); i++ )
                        {
                            InTransitLots inTransitLots = (InTransitLots) transitLots.get(i);

                            String[] waferIds = inTransitLots.getWaferId().split(";");
                            for (int wf=0; wf < waferIds.length; wf++)
                            {
                                if ( (waferId+"").equalsIgnoreCase( waferIds[wf] ) )
                                {
                                    isRmaWafer = true;
                                    break;
                                }
                            }
                            if(isRmaWafer)
                            {
                                break;
                            }
                        }
                    }
                    sortBinLot.setCustProduct( partNumber );
                    sortBinLot.setLotId( lotNumber );
                    sortBinLot.setFabLotNumber( lotNumber );
                    sortBinLot.setWaferID( waferId+"" );
                     // WAFER QTY IN Always Same(total wafers) ,But For Each every Wafer update the Wafer Qty, the the WaferQty
                    sortBinLot.setQtyIn( m_sortTotalMap.get(partNumber + lotNumber +insertion).getQtyIn() +"");
                    sortBinLot.setTotalgoodDieQtyIn( m_sortTotalMap.get(partNumber + lotNumber+insertion).getTotalgoodDieQtyIn() );
                    sortBinLot.setTotalprobeQtyIn( m_sortTotalMap.get(partNumber + lotNumber+insertion).getTotalprobeQtyIn() );
                    sortBinLot.setDateOut( StringUtils.formatDateToString(result.getDate("Test Out Date"), "MM/dd/yyyy HH:mm:ss" ) );
                    sortBinLot.setDateIn( StringUtils.formatDateToString(result.getDate("Test In Date"), "MM/dd/yyyy HH:mm:ss" ) );
                    sortBinLot.setTesterNo( result.getString("Tester ID") );
                    sortBinLot.setTestProgram( result.getString("Program No") );
                    if(isRmaWafer)
                    {
                        sortBinLot.setGoodDieQtyIn(0);
                    }
                    else
                    {
                        sortBinLot.setGoodDieQtyIn(result.getInt("good Dies"));
                    }
                    sortBinLot.setRejectedQtyIn(result.getInt("GDPW")-result.getInt("good Dies"));
                    sortBinLot.setShipYield((result.getDouble("Yield")*100)+"".replaceAll("%","") );
                    sortBinLot.setTotalYield( 0 ); // if sets as 0, in the PROC Yield Calculating (INQTY *100.00/OUTQTY)
                    sortBinLot.setBinNumber( "Bin1" );

                    cpYieldStatus.setProduct( partNumber );
                    cpYieldStatus.setLotNumber(lotNumber );
                    cpYieldStatus.setFablotNumber( fablotNumber );
                    cpYieldStatus.setTemp(result.getString("Temperature"));
                    cpYieldStatus.setProbeId(result.getString("Prober ID"));
                    cpYieldStatus.setProbeCard(result.getString("Probe Card"));
                    cpYieldStatus.setInsertion(insertion);
                    cpYieldStatus.setGrossDice( result.getInt("GDPW")+"");
                    cpYieldStatus.setGoodDice( result.getInt("good Dies")+"");
                    cpYieldStatus.setShipYield( result.getString("Yield").replaceAll("%","") );
                    cpYieldStatus.setWaferId( waferId+"");
                    cpYieldStatus.setDateIn( StringUtils.formatDateToString(result.getDate("Test In Date"), "MM/dd/yyyy HH:mm:ss" ) );
                    int[] binQty = new int[100];
                    int binNo = 0;
                    for(int i=1; i<=100; i++ )
                    {
                        binQty[binNo++] = result.getInt("Bin "+i);
                    }
                    cpYieldStatus.setBinQty( binQty );

                    cpYieldList.add( cpYieldStatus );
                }
            }
        }
        catch ( Exception ex )
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the GTC CP Yield Report");
            ex.printStackTrace();
            throw ex;
        }

        return cpYieldList;
    }
    private void parseFtProcessWipList( ArrayList wipdataList ) throws Exception
    {
        if ( wipdataList == null || wipdataList.size() == 0 )
        {
            System.out.println("\n\n********* WIP Data was not found ******\n");
            return;
        }
        GTCWipInfo wipStatus  = null;
        String partNumber       = null;
        String lotNumber        = null;
        String fabLotNumber     = null;
        TestLot testLot   = null;
        InventoryLot inventoryLot = null;
        try
        {
            Iterator iterator = wipdataList.iterator();

            while ( iterator.hasNext() )
            {
                wipStatus = ( GTCWipInfo ) iterator.next();

                partNumber = wipStatus.getPartNumber();
                lotNumber  = wipStatus.getLotNumber();
                fabLotNumber = wipStatus.getFablotNumber();

                if (lotNumber == null)
                {
                    lotNumber = fabLotNumber;
                }
                String dateIn = null;
                String expectedDateOut = null;
                //2008/05/13 19:13:17,132
                if ( wipStatus.getDateIn() != null )
                {
                     dateIn = StringUtils.formatDate(wipStatus.getDateIn(),"yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy" );
                }
                else if ( wipStatus.getReceiveDate() != null )
                {
                     dateIn = StringUtils.formatDate(wipStatus.getReceiveDate(),"yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy" );
                }

                if ( wipStatus.getFtOutDate() != null )
                {
                     expectedDateOut = StringUtils.formatDate(wipStatus.getFtOutDate(),"yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy" );
                }
                else if ( wipStatus.getExpectedDateOut() != null )
                {
                     expectedDateOut = StringUtils.formatDate(wipStatus.getExpectedDateOut(),"yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy" );
                }
                String lotStatus = null;
                if( wipStatus.getStatus() != null && ( "WAIT".equalsIgnoreCase( wipStatus.getStatus())
                        || "HELD".equalsIgnoreCase( wipStatus.getStatus()) || wipStatus.getStatus().indexOf("HLD") != -1 ) )
                {
                    lotStatus = LOT_STATUS_HOLD;
                }
                else
                {
                    lotStatus = LOT_STATUS_RUNNING;
                }

                if ( partNumber != null && partNumber.trim().length() > 0 && fabLotNumber != null && fabLotNumber.trim().length() > 0 )
                {
					if ( wipStatus.getStage() != null && !"Bank".equalsIgnoreCase( wipStatus.getStage() ) && !"VM".equalsIgnoreCase( wipStatus.getStage() )
                            && !"Pack".equalsIgnoreCase( wipStatus.getStage() ) && !"Outsourcing".equalsIgnoreCase( wipStatus.getStage() ) )
                    {
                        //log(lotNumber + "In Test FT");

                        testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                        if (testLot == null)
                        {
                            testLot = new TestLot(VENDOR_NAME_GTC, LOCATION_TYPE_FT );
                            m_ftDataMap.put(partNumber + lotNumber + fabLotNumber, testLot);
                        }

                        int quantity = 0;
                        if (testLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(testLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getLotQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getLotQty() );
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }

                        if ( quantity > 0 )
                        {
                            testLot.setPartNumber( partNumber );
                            testLot.setLotNumber( lotNumber );
                            testLot.setFabLotNumber( fabLotNumber );
                            testLot.setDateIn( dateIn );
                            testLot.setExpecetedDate( expectedDateOut );
                            testLot.setStage( wipStatus.getStage() );
                            testLot.setQtyIn( quantity + "");
                            testLot.setPackageType( wipStatus.getPkgType() );
                            testLot.setStatus(lotStatus);
                            testLot.setHoldCode( wipStatus.getHoldCode() );
                            testLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            testLot.setDateCode( wipStatus.getDateCode() );
                        }
                    }
                    if ( wipStatus.getStage() != null && ( "VM".equalsIgnoreCase( wipStatus.getStage() )
                            || "Pack".equalsIgnoreCase( wipStatus.getStage() ) || "Outsourcing".equalsIgnoreCase( wipStatus.getStage() ) ) )
                    {
                        inventoryLot = (InventoryLot) m_eolDataMap.get( partNumber + lotNumber + fabLotNumber);

                        if (inventoryLot == null)
                        {
                            inventoryLot = new InventoryLot(VENDOR_NAME_GTC, LOCATION_TYPE_EOL );
                            m_eolDataMap.put( partNumber + lotNumber + fabLotNumber, inventoryLot);
                        }

                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getLotQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getLotQty());
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }
                        if ( quantity > 0 )
                        {
                            inventoryLot.setPartNumber(partNumber);
                            inventoryLot.setLotNumber(lotNumber);
                            inventoryLot.setQtyIn(quantity + "");
                            inventoryLot.setDateIn(dateIn);
                            inventoryLot.setExpectedDate(expectedDateOut);
                            inventoryLot.setFabLotNumber(fabLotNumber);
                            inventoryLot.setStatus(lotStatus);
                            inventoryLot.setHoldReason( wipStatus.getHoldReason() );
                            inventoryLot.setCategory(LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setStage( wipStatus.getStage() );
                            inventoryLot.setDateCode( wipStatus.getDateCode() );
                        }

                    }

                    if (  wipStatus.getStage() != null && "Bank".equalsIgnoreCase( wipStatus.getStage() ) )
                    {
                        //log( lotNumber +" In Pre Test");

                        inventoryLot = (InventoryLot)m_preTestDataMap.get( partNumber + fabLotNumber + lotNumber );

                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot( VENDOR_NAME_GTC, LOCATION_TYPE_PRE_TEST );
                            m_preTestDataMap.put( partNumber + fabLotNumber + lotNumber, inventoryLot );
                        }

                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getLotQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getLotQty() );
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }
                        if (quantity > 0 )
                        {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( lotNumber );
                            inventoryLot.setQtyIn(quantity + "");
                            inventoryLot.setDateIn( dateIn );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setPkgType( wipStatus.getPkgType() );
                            inventoryLot.setStatus( lotStatus );
                            inventoryLot.setStage( wipStatus.getStage() );
                            inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setDateCode( wipStatus.getDateCode() );
                        }
                    }

                }
            }
        }
        catch(Exception e)
        {
            System.out.println(" Error while parsing the Global Test wip status " + e);
            e.printStackTrace();
            throw e;
        }
    }

    private void parseFGProcessShipList(ArrayList shippingDataList) throws Exception
    {
       if ( shippingDataList == null || shippingDataList.size() == 0 )
       {
            return;
       }
       GTCShipInfo shipStatus  = null;
       String partNumber           = null;
       String lotNumber            = null;
       ShippedLot shippedLot       = null;
       String fabLotNumber         = null;
       String nextLocation         = null;
       try
       {
           Iterator iterator = shippingDataList.iterator();

           while ( iterator.hasNext() )
           {
               shipStatus = (GTCShipInfo) iterator.next();

               lotNumber    = shipStatus.getLotNumber();
               partNumber   = shipStatus.getCustPartNo();
               fabLotNumber = shipStatus.getLotNumber();
               nextLocation = shipStatus.getShipToLocation();
               String shipDate = shipStatus.getShipDate();

               //FG SHIP

               //log("In FG Shippping " + lotNumber );
               shippedLot = ( ShippedLot ) m_fgShipDataMap.get ( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate );

               if ( shippedLot == null )
               {
                    shippedLot = new ShippedLot(VENDOR_NAME_GTC, LOCATION_TYPE_FINISHED_GOODS);
                    m_fgShipDataMap.put( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate, shippedLot );
               }

               int quantity = 0;
               if ( shippedLot.getShipQty() != null )
               {
                   try
                   {
                       quantity = Integer.parseInt( shippedLot.getShipQty() );
                   }
                   catch ( NumberFormatException ignore )
                   {
                       quantity = 0;
                   }
                   try
                   {
                       quantity += Integer.parseInt( shipStatus.getShipDieQty() );
                   }
                   catch ( NumberFormatException ignore )
                   {
                       quantity = 0;
                   }
               }
               else
               {
                   try
                   {
                       quantity = Integer.parseInt( shipStatus.getShipDieQty() );
                   }
                   catch ( NumberFormatException ignore )
                   {
                       quantity = 0;
                   }
               }

               if ( quantity > 0 )
               {
                   shippedLot.setLotNumber( lotNumber );
                   shippedLot.setPartNumber( partNumber );
                   shippedLot.setShipDate( shipDate );
                   shippedLot.setShipQty( quantity + "" );
                   shippedLot.setNextLocation( nextLocation );
                   shippedLot.setFabLotNumber( fabLotNumber );
                   shippedLot.setCustomerPO( shipStatus.getPoNumber() );
                   shippedLot.setCustomerSO( shipStatus.getSoNumber() );
                   shippedLot.setProcessType( LOCATION_TYPE_FG );
				   shippedLot.setShippingDoc( shipStatus.getSrNumber() );
               }
           }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            log("Exception while geting GTC FG Shipping inforamtion" + e);
            throw  e;
        }
    }

    private void parseFGProcessWipList( ArrayList wipdataList ) throws Exception
    {
        if ( wipdataList == null || wipdataList.size() == 0 )
        {
            System.out.println("\n\n********* WIP Data was not found ******\n");
            return;
        }
        GTCWipInfo wipStatus  = null;
        String partNumber       = null;
        String lotNumber        = null;
        String fabLotNumber     = null;
        InventoryLot inventoryLot = null;
        try
        {
            Iterator iterator = wipdataList.iterator();

            while ( iterator.hasNext() )
            {
                wipStatus = ( GTCWipInfo ) iterator.next();

                partNumber = wipStatus.getPartNumber();
                lotNumber  = wipStatus.getLotNumber();
                fabLotNumber = wipStatus.getFablotNumber();

                if (lotNumber == null)
                {
                    lotNumber = fabLotNumber;
                }

                if ( partNumber != null && partNumber.trim().length() > 0 && fabLotNumber != null && fabLotNumber.trim().length() > 0 )
                {

                    inventoryLot = (InventoryLot)m_fgDataMap.get( partNumber + fabLotNumber + lotNumber );

                    if ( inventoryLot == null )
                    {
                        inventoryLot = new InventoryLot( VENDOR_NAME_GTC, LOCATION_TYPE_FG );
                        m_fgDataMap.put( partNumber + fabLotNumber + lotNumber, inventoryLot );
                    }

                    int quantity = 0;
                    if (inventoryLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt(inventoryLot.getQtyIn());
                        }
                        catch (NumberFormatException ignore) {
                        }
                        try
                        {
                            quantity += Integer.parseInt(wipStatus.getDieQtyIn() );
                        }
                        catch (NumberFormatException nfe) {
                        }
                    }
                    else
                    {
                        try
                        {
                            quantity = Integer.parseInt( wipStatus.getDieQtyIn() );
                        }
                        catch (NumberFormatException ignore) {
                        }
                    }

                    if (quantity > 0 )
                    {
                        inventoryLot.setPartNumber( partNumber );
                        inventoryLot.setLotNumber( lotNumber );
                        inventoryLot.setQtyIn(quantity + "");
                        inventoryLot.setDateIn( wipStatus.getReceiveDate() );
                        inventoryLot.setFabLotNumber( fabLotNumber );
                        inventoryLot.setPkgType( wipStatus.getPkgType() );
                        inventoryLot.setStatus( LOT_STATUS_RUNNING );
                        inventoryLot.setDateCode( wipStatus.getDateCode() );
                        inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                    }
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(" Error while parsing the Global Test wip status " + e);
            e.printStackTrace();
            throw e;
        }
    }
    /**
     *
     * @param wipdataList
     * @throws Exception
     */
    public void parseSortProcessWipList( ArrayList wipdataList ) throws Exception
    {
        if ( wipdataList == null || wipdataList.size() == 0 )
        {
            System.out.println("\n\n********* WIP Data was not found ******\n");
            return;
        }
        GTCWipInfo wipStatus  = null;
        String partNumber       = null;
        String lotNumber        = null;
        String fabLotNumber     = null;
        FabSortLot fabSortLot   = null;
		InventoryLot wsInventoryLot = null;

        try
        {
            Iterator iterator = wipdataList.iterator();

            while ( iterator.hasNext() )
            {
                wipStatus = ( GTCWipInfo ) iterator.next();

                partNumber = wipStatus.getPartNumber();
                lotNumber  = wipStatus.getLotNumber();
                fabLotNumber = wipStatus.getFablotNumber();

                if (lotNumber == null)
                {
                    lotNumber = fabLotNumber;
                }
                if ( partNumber != null && partNumber.trim().length() > 0 && fabLotNumber != null && fabLotNumber.trim().length() > 0 )
                {
					//WAFER STORE
					if ( wipStatus.getStatus() != null && "Receive".equalsIgnoreCase( wipStatus.getStatus() ) )
					{
						 wsInventoryLot = (InventoryLot) m_waferBankDataMap.get(partNumber + fabLotNumber);
                        if (wsInventoryLot == null)
                        {
                            wsInventoryLot = new InventoryLot(VENDOR_NAME_GTC, LOCATION_TYPE_WAFER_STORE );
                            m_waferBankDataMap.put(partNumber + fabLotNumber, wsInventoryLot);
                        }

                        int quantity = 0;
                        if (wsInventoryLot.getWaferQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt( wsInventoryLot.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                        }

                        if (quantity > 0)
                        {
                            wsInventoryLot.setLotNumber(fabLotNumber);
                            wsInventoryLot.setPartNumber(partNumber);
                            wsInventoryLot.setDateIn( wipStatus.getReceiveDate() );
                            wsInventoryLot.setWaferQtyIn(quantity + "");
                            wsInventoryLot.setStatus(LOT_STATUS_RUNNING);
                            wsInventoryLot.setFabLotNumber(fabLotNumber);
                            wsInventoryLot.setCategory(LOT_CATEGORY_TYPE_PROD);
                        }

					}
					//SORT
                    if ( wipStatus.getStatus() != null && !"Receive".equalsIgnoreCase( wipStatus.getStatus() ) )
                    {
                        fabSortLot = ( FabSortLot ) m_sortWipDataMap.get( partNumber + fabLotNumber);
                        if ( fabSortLot == null )
                        {
                            fabSortLot = new FabSortLot( VENDOR_NAME_GTC, WIP_TYPE_SORT );
                            m_sortWipDataMap.put( partNumber + fabLotNumber , fabSortLot );
                        }

                        int quantity = 0;
                        if (fabSortLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt( fabSortLot.getQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                        }

                        if ( quantity > 0  )
                        {
                            fabSortLot.setLotId( fabLotNumber );
                            fabSortLot.setCustProduct( partNumber );
                            fabSortLot.setStage( wipStatus.getStage() );
                            fabSortLot.setFabLotNumber( fabLotNumber );
                            fabSortLot.setQtyIn( quantity +"");
                            fabSortLot.setLotCategory ( LOT_CATEGORY_TYPE_PROD );
                            if ( wipStatus.getDateIn() != null )
                            {
                                fabSortLot.setDateIn(  wipStatus.getDateIn() ) ;
                            }
                            else
                            {
                                 fabSortLot.setDateIn(  wipStatus.getReceiveDate() ) ;
                            }
                            fabSortLot.setTestProgram( wipStatus.getTestPgm() );
                            if ( wipStatus.getStatus() != null && "Hold".equalsIgnoreCase( wipStatus.getStatus() ) )
                            {
                                fabSortLot.setLotStatus( LOT_STATUS_HOLD );
                                fabSortLot.setHoldReason( wipStatus.getHoldReason() );
                            }
                            else
                            {
                                fabSortLot.setLotStatus( LOT_STATUS_RUNNING );
                            }
                        }
                    }
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(" Error while parsing the Global Test wip status " + e);
            e.printStackTrace();
            throw e;
        }
    }

    private void parseSortProcessShipList(ArrayList shippingDataList) throws Exception
    {
       if ( shippingDataList == null || shippingDataList.size() == 0 )
       {
            return;
       }
       GTCShipInfo shipStatus  = null;
       String partNumber           = null;
       String lotNumber            = null;
       ShippedLot shippedLot       = null;
       String nextLocation         = null;
       try
       {
           Iterator iterator = shippingDataList.iterator();

           while ( iterator.hasNext() )
           {
               shipStatus = (GTCShipInfo) iterator.next();

               lotNumber    = shipStatus.getLotNumber() ;
               partNumber   = shipStatus.getCustPartNo();
               nextLocation = shipStatus.getShipToLocation();
               String shipDate =  shipStatus.getShipDate();

               if ( lotNumber != null && partNumber != null  )
               {
                   //log("In Shippping " + lotNumber );
                   shippedLot = ( ShippedLot ) m_sortShipDataMap.get ( partNumber + lotNumber + nextLocation + shipDate );

                   if ( shippedLot == null )
                   {
                        shippedLot = new ShippedLot(VENDOR_NAME_GTC, LOCATION_TYPE_SORT);
                        m_sortShipDataMap.put( partNumber + lotNumber + nextLocation + shipDate, shippedLot );
                   }

                   int quantity = 0;
                   int dquantity = 0;
                   if ( shippedLot.getShipQty() != null )
                   {
                       try
                       {
                           dquantity = Integer.parseInt( shippedLot.getShipDieQty() );
                           quantity = Integer.parseInt( shippedLot.getShipQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                           dquantity =0;
                       }
                       try
                       {
                           quantity += Integer.parseInt( shipStatus.getShipWaferQty() );
                           dquantity += Integer.parseInt( shipStatus.getShipDieQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                           dquantity =0;
                       }
                   }
                   else
                   {
                       try
                       {
                           quantity = Integer.parseInt( shipStatus.getShipWaferQty() );
                           dquantity = Integer.parseInt( shipStatus.getShipDieQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                           dquantity =0;
                       }
                   }
                   String waferIds = null;
                   if ( shippedLot.getWaferID() != null )
                   {
                         waferIds =  shippedLot.getWaferID();

                        waferIds = waferIds + ";" + shipStatus.getWaferID();
                   }
                   else
                   {
                       waferIds = shipStatus.getWaferID();
                   }
                         
                   if ( quantity > 0 )
                   {
                       shippedLot.setLotNumber( lotNumber );
                       shippedLot.setPartNumber( partNumber );
                       shippedLot.setShipDate( shipDate );
                       shippedLot.setShipQty( quantity + "" );
                       shippedLot.setNextLocation( nextLocation );
                       shippedLot.setWaferID( waferIds );
                       shippedLot.setFabLotNumber( lotNumber );
                       shippedLot.setProcessType( LOCATION_TYPE_SORT );
                       shippedLot.setShipDieQty( dquantity + "");
                   }
               }
           }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            log("Exception while geting GTC SORT Shipping inforamtion" + e);
            throw  e;
        }
    }
    private static void log( String message )
    {

        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            synchronized ( m_parserLogger )
            {
                m_parserLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}
